package velox.jcr.spring;

import java.io.IOException;

import javax.jcr.Credentials;
import javax.jcr.SimpleCredentials;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

public class SimpleCredentialsFactoryBean implements FactoryBean,
		InitializingBean {

	private SimpleCredentials credentials;

	private String username;
	private String password;

	public Object getObject() throws Exception {
		return credentials;
	}

	public Class<?> getObjectType() {
		return (this.credentials != null) ? this.credentials.getClass()
				: Credentials.class;
	}

	public boolean isSingleton() {
		return true;
	}

	public void afterPropertiesSet() throws Exception {
		createCredentials();
	}

	protected void createCredentials() throws IOException {
		credentials = new SimpleCredentials(username, password.toCharArray());
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}